import moment from 'moment'

const mileageUnit = ['m','km']
const oilUnit = ['ml','L']
const timeInterval = [1000,60,60]
const timeUnit = ['asDays','asHours','asMinutes','asSeconds']
const timeUnit_2 = ['days','hours','minutes','seconds']
const timeUnit_3 = ['天','小时','分钟','秒']
const timeUnit_4 = ['ms','s','m','h']



//v 数值 u开始单位 e 结束单位 unit 单位数组 x 单位间隔
const getNum = (v=0,u=0,e,unit=[],x =10000)=>{
	if(v >= x&&u<e){
		let a = v%1000;
		v = a > 0?parseInt(v/1000)+1:v/1000;
		return getNum(v,++u,e,unit,x);
	}
	v = v || 0;
	return `${v}${unit[u]}`;
}

//v：值，u：初始数据单位，nou: 返回时的单位 maxu: 最大单位
// const getTime = (v=0,u=0,nou=0)=>{
// 	if(timeInterval[u]&&v>=timeInterval[u]){
// 		let a = v%timeInterval[u];
// 		let s = a>0&&nou<=u?a+timeUnit[u]:''
// 		return `${getTime((v-a)/timeInterval[u],++u,nou)}${s}`
// 	}else if(v===0){
// 		v = v||0;
// 		return v+timeUnit[nou+1];
// 	}else if(timeInterval[u]){
// 		let a = v%timeInterval[u];
// 		let s = a>0&&nou<=u?a+timeUnit[u]:''
// 		return `${s}`
// 	}else{
// 		return `${v}${timeUnit[u]}`;
// 	}
// }

const getTime = (v,k,i,max)=>{
	let d = parseInt(moment.duration(v,timeUnit_4[k])[timeUnit[i]]());
	if(d === 0&&i < 3) return getTime(v,k,i+1,max);
	let s = d+timeUnit_3[i];
	for(let a=i+1;a<=max;a++){
		s += moment.duration(v,timeUnit_4[k])[timeUnit_2[a]]()+timeUnit_3[a];
	}
	return s;
}



export default {
	mileage(v,u,e=mileageUnit.length-1,x){
		return `${(v/1000).toFixed(2)*100/100}km`;

		// return v/Math.pow(1000,e)>1000?getNum(v,u,e,mileageUnit,x):`${parseInt(v/1000*100)/100}km`;
	},
	time(v,i=0,max=3){
		return v?getTime(v,i,0,max):`0${timeUnit_3[max]}`;
	},
	oil(v,u,e=oilUnit.length-1){
		// return getNum(v,u,e,oilUnit,1000);
		// return `${parseInt(v/1000*100)/100}L`;
		return `${(v/1000).toFixed(2)*100/100}L`;
	}
}
